package com.db.dbcloud;

import java.util.ArrayList;
import java.util.List;

import com.db.dbcloud.RTPullListView.OnRefreshListener;
import com.db.utils.ButtonStyleUtil;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ServiceActivity extends FinalActivity{
	private static final int INTERNET_FAILURE = -1;
	private static final int LOAD_SUCCESS = 1;
	private static final int LOAD_MORE_SUCCESS = 3;
	private static final int NO_MORE_INFO = 4;
	private static final int LOAD_NEW_INFO = 5;
	private List<String> dataList;
	private ArrayAdapter<String> adapter;
	
	@ViewInject(id = R.id.file_bar_return)private ImageButton retBtn;
	@ViewInject(id = R.id.file_bar_title)private TextView titleText;
	@ViewInject(id = R.id.file_bar_upload_btn, click = "clickUploadBtn")private Button uploadBtn;
	@ViewInject(id = R.id.pullListView)private RTPullListView pullListView;
	private ProgressBar moreProgressBar;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_service);
		retBtn.setVisibility(View.INVISIBLE);
		
		ButtonStyleUtil.setButtonFocusChanged(uploadBtn);
		
		titleText.setText("碉堡云盘");
		dataList = new ArrayList<String>();
		for (int i = 0; i < 5; i++) {
			dataList.add("Item data "+i);
		}
		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dataList);
		pullListView.setAdapter(adapter);
		
		pullListView.setonRefreshListener(new OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				new Thread(new Runnable() {
				
				@Override
				public void run() {
					try {
						Thread.sleep(2000);
						dataList.clear();
						for (int i = 0; i < 5; i++) {
							dataList.add("Item data "+i);
						}
						myHandler.sendEmptyMessage(LOAD_NEW_INFO);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}).start();
			}
		});
	}
	private Handler myHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case LOAD_MORE_SUCCESS:
				moreProgressBar.setVisibility(View.GONE);
				adapter.notifyDataSetChanged();
				pullListView.setSelectionfoot();
				break;

			case LOAD_NEW_INFO:
				adapter.notifyDataSetChanged();
				pullListView.onRefreshComplete();
				break;
			default:
				break;
			}
		}
    	
    };
    public void clickUploadBtn(View v){
    	Intent intent = new Intent(ServiceActivity.this.getParent(), FileActivity.class);
    	this.getParent().startActivity(intent);
    }
}
